/*
 * Utility.cpp
 *
 *  Created on: 07.11.2010
 *      Author: 123
 */
#include "Utility.h"
int CollisionDetection(int x1, int y1, int w1, int h1, int x2, int y2, int w2,
		int h2) {
	int Collide = 0;

	if ((y2 >= y1 && y1 + h1 >= y2) || (y2 + h2 >= y1 && y1 + h1 >= y2 + h2)
			|| (y1 >= y2 && y2 + h2 >= y1) || (y1 + h1 >= y2 && y2 + h2 >= y1
			+ h1))

	{

		if (x2 >= x1 && x1 + w1 >= x2) {
			Collide = 1;
		}

		if (x2 + w2 >= x1 && x1 + w1 >= x2 + w2) {
			Collide = 1;
		}

		if (x1 >= x2 && x2 + w2 >= x1) {
			Collide = 1;
		}

		if (x1 + w1 >= x2 && x2 + w2 >= x1 + w1) {
			Collide = 1;
		}

	}

	return Collide;

}
int freememory() {
	void* buf[128];
	int i = 0;
	for (i = 0; i < 128; i++) {
		buf[i] = malloc(1024 * 512);
		if (!buf[i])
			break;
	}
	int result = i;
	for (; i >= 0; i--) {
		free(buf[i]);
	}
	return result * 1024 * 512;
}
void drawBar(int xloc, int xsiz, int yloc, int ysiz, int color1, int color2,
		int color3) {
	oslDrawGradientRect(xloc, yloc, xsiz, ysiz, RGB(color1, color2, color3),
			RGB(color1, color2, color3), RGB(255, 255, 255),
			RGB(255, 255, 255));
	oslDrawGradientRect(xloc, yloc + 5, xsiz, ysiz + 5, RGB(225, 255, 255),
			RGB(255, 255, 255), RGB(color1, color2, color3),
			RGB(color1, color2, color3));
}

void showBatteryBar(void) {
	float barLife = 0;
	int i;
	if (scePowerIsBatteryExist())
		barLife = scePowerGetBatteryLifePercent();
	//barLife = 23;


	if (barLife > 50)
		drawBar(479 - barLife / 4, 479, 3, 8, 40, 225, 0);
	else if (barLife > 25)
		drawBar(479 - barLife / 4, 479, 3, 8, 255, 225, 0);
	else
		drawBar(479 - barLife / 4, 479, 3, 8, 255, 0, 0);
	oslDrawRect(479 - barLife / 4, 2, 479, 14, RGB(0, 0, 0));
	oslDrawRect(478 - 25, 2, 479, 14, RGB(0, 0, 0));
	for (i = 0; i < 3; i++)
		oslDrawLine(477 - 25 - i, 4 + i, 477 - 25 - i, 12 - i, RGB(0, 0, 0));
}

